package com.web.gatewayservice.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;

/**
 * 自定义网关过滤器
 */
@Component
public class HelloGatewayFilterFactory
        extends AbstractGatewayFilterFactory<HelloGatewayFilterFactory.Config> {

    public HelloGatewayFilterFactory(){
        //将参数配置类型传入过滤器
        super(Config.class);
    }

    //过滤器的业务逻辑
    @Override
    public GatewayFilter apply(Config config) {
        return ((exchange, chain) -> {
            System.out.println("你好！！！！" + config.getName());
            //放行请求到下一个过滤器
            return chain.filter(exchange);
        });
    }

    /**
     * 自定义参数的配置类
     */
    public static class Config{
        //自定义参数
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
}
